<template>
  <div class="attr-list">
    <CommonAttr></CommonAttr>
    <el-table :data="tableData" border stripe size="mini" class="dec-scrollbar">
      >
      <el-table-column v-for="(item, i) in theadData" :key="i" :label="item.label" :width="item.width ? item.width : ''">
        <!-- 自定义表头 -->
        <template slot="header" slot-scope="scope">
          <el-input v-if="thead[i]" :ref="`thead${i}Ref`" v-model="theadData[i].label" @change="handleTheadInputConfirm(i)"></el-input>
          <span class="context" @dblclick="showTheadInput(i)" v-else>{{ scope.column.label }}</span>
        </template>
        <!-- 自定义表格 -->
        <template slot-scope="scope">
          <el-input v-if="table[scope.$index][i]" :ref="`${scope.$index}${i}Ref`" v-model="scope.row[item.propName]" @change="handleInputConfirm(scope.$index, i)"></el-input>
          <span class="context" @dblclick="showInput(scope.$index, i)" v-else>
            {{ scope.row[item.propName] }}
          </span>
        </template>
      </el-table-column>
    </el-table>
    <el-row class="btns">
      <el-col :span="6"><el-button type="primary" size="mini" @click="addRow">添加行</el-button></el-col>
      <el-col :span="6"><el-button type="primary" size="mini" @click="addCol">添加列</el-button></el-col>
      <el-col :span="6"><el-button type="primary" size="mini" @click="deleteRow">删除行</el-button></el-col>
      <el-col :span="6"><el-button type="primary" size="mini" @click="deleteCol">删除列</el-button></el-col>
    </el-row>

    <el-divider>表格行样式</el-divider>
    <el-form ref="styleFormRef" :model="tablePropValue">
      <el-form-item label="背景颜色">
        <el-color-picker v-model="tablePropValue.rowStyle.backgroundColor" show-alpha size="mini"></el-color-picker>
      </el-form-item>
      <el-form-item label="左右对齐">
        <el-select v-model="tablePropValue.align">
          <el-option v-for="item in textAlignOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="斑马纹">
            <el-switch v-model="tablePropValue.stripe" active-color="#13ce66" inactive-color="#dcdfe6"> </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="表头加粗">
            <el-switch v-model="isWeight" active-color="#13ce66" inactive-color="#dcdfe6" @change="changeFontWeight"> </el-switch>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { textAlignOptions } from '@/components/Attribute/Attr.js'
import Vue from 'vue'
import CommonAttr from '@/components/Attribute/CommonAttr.vue'

export default {
  computed: {
    ...mapState(['curComponent']),
    tablePropValue() {
      return this.curComponent.propValue
    }
  },
  data() {
    return {
      tableData: [],
      theadData: [],
      rowLen: 0,
      colLen: 0,
      // table记录当前表格每一项的选中状态
      table: [],
      // thead记录当前表头的选中状态
      thead: [],
      nextId: 0,
      textAlignOptions,
      // 记录是否表头文字加粗
      isWeight: true
    }
  },
  created() {
    this.initData()
  },
  components: {
    CommonAttr
  },
  methods: {
    initData() {
      if (this.curComponent == null) return
      this.tableData = this.curComponent.propValue.tableData
      this.theadData = this.curComponent.propValue.theadData
      this.reset()
    },
    // 重置table thead状态
    reset() {
      this.rowLen = this.tableData.length
      this.colLen = this.theadData.length
      this.table = []
      // 初始化table
      for (let i = 0; i < this.rowLen; i++) {
        let colArr = new Array(this.colLen).fill(false)
        this.table.push(colArr)
      }
      // 初始化thead
      this.thead = new Array(this.colLen).fill(false)
    },
    // 处理表格
    showInput(rowIndex, colIndex) {
      Vue.set(this.table[rowIndex], colIndex, true)
      this.$nextTick(() => {
        this.$refs[`${rowIndex}${colIndex}Ref`][0].$refs.input.focus()
      })
    },
    handleInputConfirm(rowIndex, colIndex) {
      Vue.set(this.table[rowIndex], colIndex, false)
    },
    // 处理表头
    showTheadInput(colIndex) {
      Vue.set(this.thead, colIndex, true)
      this.$nextTick(() => {
        this.$refs[`thead${colIndex}Ref`][0].$refs.input.focus()
      })
    },
    handleTheadInputConfirm(colIndex) {
      Vue.set(this.thead, colIndex, false)
    },
    addRow() {
      let newRow = {}
      this.theadData.forEach((item) => {
        newRow[item.propName] = ''
      })
      this.tableData.push(newRow)
      this.reset()
    },
    addCol() {
      let newCol = {
        propName: 'col' + this.nextId,
        label: ''
      }
      this.nextId++
      this.theadData.push(newCol)
      this.reset()
    },
    deleteRow() {
      this.tableData.pop()
      this.reset()
    },
    deleteCol() {
      this.theadData.pop()
      this.reset()
    },
    changeFontWeight(state) {
      if (state == true) {
        this.tablePropValue.headerCellStyle.fontWeight = 700
      } else {
        this.tablePropValue.headerCellStyle.fontWeight = 400
      }
    }
  }
}
</script>

<style lang="less" scoped>
.context {
  display: inline-block;
  min-height: 20px;
  min-width: 100%;
  cursor: pointer;
}

.btns {
  margin-top: 20px;
}

/deep/ .el-form-item__label {
  text-align: left;
}

.el-form-item {
  margin-bottom: 10px;
}
</style>
